from append_trunk_to_PYTHONPATH import preparePath
preparePath()

import unittest
from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart
from playerRelated.inventory import Inventory

class InventoryTests(unittest.TestCase):
    
    inventory = None
    
    def setUp(self):
        self.inventory = Inventory(DirectObject())
    
    
    def test_initializingWithDirectObject(self):
        
        self.failUnless(
            isinstance(self.inventory._directObjectParent, DirectObject)
        )
        
        
    def test_addingNewItems(self):
        str = 'Spam ex xxxxx3333 34343434 ][;x34'
        self.inventory.addToInventory(str)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 1)
        
        str2 = '746764b7v58c4yt748nydt74ytc748wny6 x83      '
        self.inventory.addToInventory(str2, 56)
        self.failUnless(str2 in self.inventory._items)
        self.failUnless(self.inventory._items[str2] == 56)
        
        
    def test_addingExistingItems(self):
        str = 'Peachlinger'
        self.inventory.addToInventory(str)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 1)
        
        self.inventory.addToInventory(str)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 2)
        
        self.inventory.addToInventory(str, 3)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 5)
        
        
    def test_removeNonExistingItems(self):
        str = 'Who\'s that hiding in the treetops?'
        self.failUnless(
            self.inventory.takeFromInventory(str) == None
        )
        
        
    def test_removingMoreItemsThanPossesed(self):
        str = 'Crocodile in pink tutu'
        self.inventory.addToInventory(str)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 1)
        
        self.failUnless(
            self.inventory.takeFromInventory(str, 565756) == 1
        )
        
        self.failUnless(str not in self.inventory._items)
        
        
    def test_removingExactAmountPossesed(self):
        str = 'Elephant in hipster pullover'
        self.inventory.addToInventory(str, 1234567)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 1234567)
    
        self.failUnless(
            self.inventory.takeFromInventory(str, 1234567) == 1234567
        )
        
        self.failUnless(str not in self.inventory._items)
        
    def test_removingLessThanAmountPossesed(self):
        str = 'Nerdy nerd'
        self.inventory.addToInventory(str, 1234567)
        self.failUnless(str in self.inventory._items)
        self.failUnless(self.inventory._items[str] == 1234567)
    
        self.failUnless(
            self.inventory.takeFromInventory(str, 1234566) == 1234566
        )
        
        self.failUnless(str in self.inventory._items)
        
###   Now I feel like it would be good idea to store some _money in player's inventory... 
###   not directly, though, but through some kind of counter
    def test_addSomeMoney(self):
        moneyBefore = int(self.inventory._money)
        self.inventory.addMoney(234)
        moneyAfter = int(self.inventory._money)
        
        self.failUnless((moneyBefore + 234) == moneyAfter)
        
    def test_takeMoney(self):
####### case 1: amount of _money you take < amount of _money you have
        moneyBefore = int(self.inventory._money)
        self.inventory.addMoney(234)
        
        self.failUnless(self.inventory.takeMoney(230) == 230)
        moneyAfter = int(self.inventory._money)
        self.failUnless(moneyAfter == 4)
        self.inventory._money = 0 # cleaning before next test
        
####### case 2: amount of _money you take = amount of _money you have
        moneyBefore = int(self.inventory._money) # should be zero
        self.failUnless(moneyBefore == 0)
        self.inventory.addMoney(234)
        moneyAfter = int(self.inventory._money)
        self.failUnless(moneyAfter == 234)
        
        self.failUnless(self.inventory.takeMoney(234) == 234)
        moneyAfter = int(self.inventory._money) #should be 0
        self.failUnless(moneyBefore == moneyAfter)
        
####### case 3: amount of _money you take > amount of _money you have
        moneyBefore = int(self.inventory._money) # should be zero
        self.failUnless(moneyBefore == 0)
        self.inventory.addMoney(234)
        moneyAfter = int(self.inventory._money)
        self.failUnless(moneyAfter == 234)
        
        self.failUnless(self.inventory.takeMoney(235) == 234) #takeMoney should return value 
        moneyAfter = int(self.inventory._money)
        self.failUnless(moneyBefore == moneyAfter)
        
        
        
def main():
    unittest.main()
    
if __name__ == '__main__':
    main()